package com.yehebl.orm.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 线程绑定日期格式化类
 *
 * @author tzd
 * @Date 2021年3月22日下午4:54:02
 */
public class DateUtil {

    private static final ThreadLocal<Map<String, SimpleDateFormat>> threadFormat = new ThreadLocal<>();
    public static String df_yyyy_MM_dd_HH_ss_mm_SSS = "yyyy-MM-dd HH:ss:mm SSS";

    public static String getDefaultDateForMat() {
        return getSimpleDateFormat(df_yyyy_MM_dd_HH_ss_mm_SSS).format(new Date());
    }

    public static SimpleDateFormat getSimpleDateFormat(String format) {
        Map<String, SimpleDateFormat> map = threadFormat.get();
        if (map == null) {
            map = new HashMap<>();
            threadFormat.set(map);
        }
        if (!map.containsKey(format)) {
            SimpleDateFormat sf = new SimpleDateFormat(format);
            map.put(format, sf);
            return sf;
        }
        return map.get(format);
    }

    public static String getDateDay() {
        return getSimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    public static String getDateMonth() {
        return getSimpleDateFormat("yyyy-MM").format(new Date());
    }

    public static String getDateYear() {
        return getSimpleDateFormat("yyyy").format(new Date());
    }

}
